<?php
	$clients = getClients();
	
	if($_REQUEST['item'] > 0)
	{ 
		$item['id'] = $_REQUEST['item']; 
		$item = getSiteTheme($item['id']);  
		if($_GET['set_bg'] > 0)
		{
			$update  = 'UPDATE '.DB_PERFIX.'themes SET bg='.$_GET['set_bg'].' WHERE id='.$item['id'].' LIMIT 1;';
			$result = mysql_query($update);
		}
		if($_POST['delete_item'] > 0)
		{
			$result = mysql_query('DELETE FROM '.DB_PERFIX.'sites WHERE id='.$_POST['delete_item'].' LIMIT 1;');
		}
		if($_POST['copy_as_new'] > 0)
		{ $result = copySiteTemplate($item['id']); }
	}
	/*
	if($_FILES['bg_image'])
	{
		if (image_valid($_FILES['bg_image']['type']) && ($_FILES['bg_image']['name'] != ""))
		{
			if($_FILES['bg_image']["size"] < 20000)
			{
				echo "image size too big:";
				echo $_FILES['bg_image']["size"];
			}
			echo "image valid";
			$next_id = 0;
			$result = mysql_query("SELECT id FROM ".DB_PERFIX."backgrounds ORDER BY id DESC LIMIT 1;");
			while($row = mysql_fetch_array($result))
			{
				$next_id = $row['id'];
				$next_id ++;
			}
			
			$temp_name = $_FILES['bg_image']["name"];
			$extension = substr($temp_name,-3);
			$new_name = "background_".$next_id.".".$extension;
			
			move_uploaded_file($_FILES['bg_image']["tmp_name"], BGS_PATH.$new_name);
			$ImageStatus = "Stored in: " .BGS_PATH.$new_name;
			if (copy(BGS_PATH.$new_name, BGS_THUMB_PATH.$new_name)) 
			{
				smart_resize_image(BGS_THUMB_PATH.$new_name, 100, 75, true, false );
				$insert  = "INSERT INTO ".DB_PERFIX."backgrounds (`id`, `name`, `path`)"; 
				$insert .= "VALUES (NULL, '".$_POST['bg_image_name']."', '".$new_name."');";
				$result = mysql_query($insert);
				if ($result > 0)
				{
					$ImageStatus = "<strong>Image Uploaded ".$new_name."</strong>";
				}
			
			}
			$extension = "";
		}
	}
	*/
	if(($_POST['table'] != '') && ($_POST['field'] != '') && ($_POST['search'] != '') && ($_POST['replace'] != ''))
	{ $error['toolbox'] = replaceFieldStringInTableById($_POST['table'], $_POST['field'], $_POST['search'], $_POST['replace']); }
	
	switch ($_POST['action'])
	{
		case 'lista':
			if(($_POST['new_title'] != '') && ($_REQUEST['site'] > 0))
			{ addSiteTemplate($_POST['new_title'], $_REQUEST['site']); }
			
			$i=0;
			if($_POST['theme_id'])
			{
				
				foreach($_POST['theme_id'] as $id)
				{
					updateTheme($id, $_POST['title'][$i],  $_POST['theme_type'][$i],  $_POST['hfb_set'][$i], $_POST['hfb_com_set'][$i]);
					if($_POST['copy_to'][$i] > 0)
					{ copyThemeToSite($id, $_POST['copy_to'][$i]); }
					$i++;
				}
			}
			if($_POST['default'] > 0)
			{ setTableItemDefault($_POST['default'], 'site_templates', 1, 'site_default'); }
			if($_POST['dedefault'] > 0)
			{ setTableItemDefault($_POST['dedefault'], 'site_templates', 0, 'site_default'); }
			if($_POST['delete_theme'] > 0)
			{ deleteTheme($_POST['delete_theme']); } 
		break;
		
		case 'editor':
			if($item['id'] > 0)
			{   
				if($_POST['header'] == '')
				{ $_POST['header'] = -1; }
				if($_POST['header_fixed'] == '')
				{ $_POST['header_fixed'] = -1; }
				if($_POST['header_border'] == '')
				{ $_POST['header_border'] = -1; }
				if($_POST['header_hfb'] == '')
				{ $_POST['header_hfb'] = -1; }
				if($_POST['header_content'] == '')
				{ $_POST['header_content'] = -1; }
				if($_POST['footer'] == '')
				{ $_POST['footer'] = -1; }
				if($_POST['footer_fixed'] == '')
				{ $_POST['footer_fixed'] = -1; }
				if($_POST['footer_border'] == '')
				{ $_POST['footer_border'] = -1; }
				if($_POST['footer_hfb'] == '')
				{ $_POST['footer_hfb'] = -1; }
				if($_POST['footer_content'] == '')
				{ $_POST['footer_content'] = -1; }
				if($_POST['col_left'] == '')
				{ $_POST['col_left'] = -1; }
				if($_POST['col_left_border'] == '')
				{ $_POST['col_left_border'] = -1; }
				if($_POST['col_left_hfb'] == '')
				{ $_POST['col_left_hfb'] = -1; }
				if($_POST['col_left_content'] == '')
				{ $_POST['col_left_content'] = -1; }
				if($_POST['col_right'] == '')
				{ $_POST['col_right'] = -1; }
				if($_POST['col_right_border'] == '')
				{ $_POST['col_right_border'] = -1; }
				if($_POST['col_right_hfb'] == '')
				{ $_POST['col_right_hfb'] = -1; }
				if($_POST['col_right_content'] == '')
				{ $_POST['col_right_content'] = -1; }
				updateTheme($item['id'], $_POST['title'],  $_POST['theme_type'],  $_POST['hfb_set'], $_POST['hfb_com_set'], $_POST['fixed_width'], $_POST['horisontal_pos'], $_POST['GridSystem'], $_POST['Bootstrap'], $_POST['border_site'], $_POST['border_content'], $_POST['header'], $_POST['footer'], $_POST['col_left'], $_POST['col_right'], $_POST['header_fixed'], $_POST['header_border'], $_POST['header_hfb'], $_POST['header_content'], $_POST['footer_fixed'], $_POST['footer_border'], $_POST['footer_hfb'], $_POST['footer_content'], $_POST['col_left_border'], $_POST['col_left_hfb'], $_POST['col_left_content'], $_POST['col_right_border'], $_POST['col_right_hfb'], $_POST['col_right_content'] );
			}
		break;
		case 'editor_theme_css':
			if($_REQUEST['css'] > 0)
			{
				$item['id'] = $_REQUEST['css']; 
				$item = getSiteTheme($item['id']); 
				
				if($_POST['load_css'] > 0)
				{ loadThemeHFBCss($item['id'], $item['site_id'], $_POST['load_css']); }
				
				if(($_POST['new_selector'] != '') && (trim($_POST['new_style']) != ''))
				{ addThemeStyle($item['id'], $_POST['type'], $_POST['new_selector'], trim($_POST['new_style'])); }
				
				if($_POST['delete_selector'] > 0)
				{ deleteThemeCssSelector( $_POST['delete_selector'], $item['id']); }
				
				if($_POST['style_id'])
				{
					$i=0;
					foreach($_POST['style_id'] as $id)
					{
						$new_name = '';
						if($_FILES['bg_image']['name'][$i])
						{
							if (image_valid($_FILES['bg_image']['type'][$i]) && ($_FILES['bg_image']['name'][$i] != ""))
							{
								if($_FILES['bg_image']["size"][$i] > 200000)
								{
									echo "image size too big:";
									echo $_FILES['bg_image']["size"][$i];
								}
								
								$temp_name = $_FILES['bg_image']["name"][$i];
								$extension = substr($temp_name,-3);
								
								if(strpos($_POST['selector'][$i],",") > 0)
								{
									$new_name = $_POST['selector'][$i];
									$new_name = substr($new_name, 0, strpos($new_name,","));
								}
								else
								{ $new_name = $_POST['selector'][$i]; }
								
								$new_name = str_replace('#','id_',$new_name);
								$new_name = str_replace(' div','-div',$new_name);
								$new_name = str_replace('.','_',$new_name);
								$new_name = str_replace(' ','_',$new_name);
								$new_name = str_replace(':hover','_h',$new_name);
								$new_name = str_replace('.active','_a',$new_name);
								$new_name = str_replace('.','',$new_name);
								$new_name = str_replace(':','',$new_name);
								$new_name = $new_name.'_bg.'.$extension;
								
								$store_path = SITE_PATH.'templates/sites/'.$item['site_id'].'/themes/'.$item['id'].'/images/';
								
								move_uploaded_file($_FILES['bg_image']["tmp_name"][$i], $store_path.$new_name);
								$ImageStatus = "Stored in: ".$new_name;
								$extension = "";
							}
						}
						updateThemeStyle($id, $_POST['selector'][$i],  $_POST['style'][$i], $new_name);
						$i++;
					}
					saveThemeCss($item['id'], $item['site_id'], $_POST['type']);
				}
			}
		break;
	}
	if($_REQUEST['item'] > 0)
	{ 
		$item['id'] = $_REQUEST['item']; 
		$item = getSiteTheme($item['id']);  
	}
	else
	{
		if($_REQUEST['css'] > 0)
		{ $item = getSiteTheme($_REQUEST['css']); }
	}
	
?>